package com.tjyx.common.util;

import java.util.HashMap;
import java.util.Map;

import com.tjyx.common.DispatcherException;

/**
 * 转发工具类,DispactherServlet辅助类
 * 时间：2012-3-26
 * 
 * @author zhutianjie
 * @version v1.0
 *
 */
public class DispatcherUtil {
	
	/**
	 * 请求的Action路径全名配置代号
	 */
	public static final String ACTION_NAME = "an";  
	/**
	 * 调用的方法配置代号
	 */
	public static final String ACTION_METHOD = "am"; 
	/**
	 * 请求路径中传入的参数配置代号
	 */
	public static final String REQUEST_PARAMS = "rp";
	/**
	 * 请求转发的方式的配置代号
	 */
	public static final String FORWARD_METHOD = "fm";
	
	private static final String BASE_PACKEGE = "com.tjyx"; //根路径
	
	private static final Map<String, String> CONFIG_MAP = new HashMap<String, String>(); //装载请求配置的映射

	/**
	 * <P>
	 * 从请求路径中取出请求的信息<br />
	 * 包括:<br />
	 * 1.请求的Action类路径<br />
	 * 2.Action中调用的方法<br />
	 * 3.请求转发的方式
	 * 4.传入的参数信息
	 * </p>
	 * <p>
	 * 请求路径eg:'nameSpace_*Action.do.method.forwardMethod?param1=*&param2=*...s'
	 * </p>
	 * @param targetURI
	 * @return
	 * @throws DispatcherException
	 */
	public Map<String,String> checkOutRequestConfig(String targetURI) throws DispatcherException{
		int subIndex = targetURI.indexOf("/", 2);
		targetURI = targetURI.substring(subIndex + 1);
		String params = null;
		if(!targetURI.contains("_") || !targetURI.contains(".")) {
			throw new DispatcherException("转发URI错误,正确格式为nameSpace_*Action_method_forwardMethod.do?param1=*&param2=*...");
		}
		
		if(targetURI.contains("?")) {
			params = targetURI.substring(targetURI.indexOf("?")); //截取参数
			CONFIG_MAP.put(REQUEST_PARAMS, params);
			targetURI = targetURI.substring(0,targetURI.indexOf("?")); //截取请求地址及配置
		}
		
		String[] strs = targetURI.split("_");
		if(strs.length != 3 && strs.length != 4) {
			throw new DispatcherException("转发URI错误,正确格式为nameSpace_*Action_method_forwardMethod.do?param1=*&param2=*...");
		}
		return buildUpConfigMap(strs);
		
	}
	
	/**
	 * 创建配置映射
	 * @param strs
	 * @return
	 * @throws DispatcherException
	 */
	private Map<String, String> buildUpConfigMap(String[] strs) throws DispatcherException {
		int dotLocation;
		String actionMethod;
		CONFIG_MAP.put(ACTION_NAME, buildUpFullActionName(strs[0],strs[1]));
		if(strs.length == 4) {
			dotLocation = strs[3].indexOf(".");
			actionMethod = strs[2];
			CONFIG_MAP.put(FORWARD_METHOD, strs[3].substring(0,dotLocation));
		} else {
			dotLocation = strs[2].indexOf(".");
			actionMethod = strs[2].substring(0,dotLocation);
			CONFIG_MAP.put(FORWARD_METHOD, "forward");
		}
		CONFIG_MAP.put(ACTION_METHOD, actionMethod);
		return CONFIG_MAP;
	}
	
	/**
	 * 创建Action的路径全名
	 * @param nameSpace 包命名空间
	 * @param actionName 调用Action的名字
	 * @return
	 * @throws DispatcherException
	 */
	private String buildUpFullActionName(String nameSpace, String actionName) throws DispatcherException {
			char firstChar = actionName.charAt(0);
			firstChar = Character.toUpperCase(firstChar);
			actionName = firstChar + actionName.substring(1);
			String fullName = BASE_PACKEGE + "." + nameSpace + ".action." + actionName;
			return fullName;
	}
	
	public static void main(String[] args) throws DispatcherException {
		DispatcherUtil util = new DispatcherUtil();
		System.out.println(util.checkOutRequestConfig("maintain_loginAction_login_redirect.do"));
	}
	
}
